package com.cqc.locationmanager01;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private LocationManager lm;
    private LocationListener listener;
    private Location myLocation;
    private long time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        initViews();

        lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                myLocation = location;
                LogUtil.d("onLocationChanged" + location.getProvider() + "," + location.getLongitude() + "," + location.getLatitude() + ","
                        + location.getTime() + "," + location.getAltitude());
//                time=location.getTime();
//                LogUtil.d("onLocationChanged,getTime="+location.getTime());
                MyApplication.setlocationTime(location.getTime());
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                LogUtil.d("onStatusChanged," + provider + "," + status);
            }

            @Override
            public void onProviderEnabled(String provider) {
                LogUtil.d("onProviderEnabled," + provider);
            }

            @Override
            public void onProviderDisabled(String provider) {
                LogUtil.d("onProviderDisabled," + provider);
            }
        };

//        lm.removeUpdates(listener);
    }

    private void initViews() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }

    private void findViews() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
    }

    /**
     * 第一个参数：定位类型
     * 第二个参数：定位时间间隔
     * 第三个参数：定位距离间隔
     * 第四个参数：监听
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
                break;
            case R.id.btn2:
                lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 0, listener);
                break;
            case R.id.btn3:
                lm.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 3000, 0, listener);
                break;
            case R.id.btn4:
                if (myLocation == null) {
                    LogUtil.d("myLocation == null");
                } else {
//                    LogUtil.d("btn4," + myLocation.getTime() + myLocation.getLongitude() + "," + myLocation.getLatitude());
//                    LogUtil.d("btn4,time=" + time);
                    LogUtil.d("btn4,time=" + MyApplication.getlocationTime());
                }
                break;
        }
    }
}
